define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    require('./wrStatisticsPopupCtrl');


    $.widget('rk.wrStatistics_1PopupCtrl', $.rk.wrStatisticsPopupCtrl, {
        options: {
            detailUrl: '/json/oa_workreport/account-list-detail.action'
        },
        _create: function () {
            var me = this;
            var elem = me.element;
        },
        _init: function () {
            var me = this;
            me._super();
        },
        _handleData: function (data) {
            var me = this;
            var accounts = data.accounts;
            var salesParameters = data.salesParameters ? data.salesParameters : [];//字典
            salesParameters = me._getParameters(salesParameters);

            if (accounts.length > 0)accounts[accounts.length - 1].isLast = true;
            //获取统计数据的文字描述
            for (var i = 0; i < accounts.length; i++) {
                var acc = accounts[i];
                if (!acc.statistic || acc.statistic.length == 0) {
                    acc.hasdata = false;
                } else {
                    acc.hasdata = true;
                }
                if (acc.statistic) {
                    for (var j = 0; j < acc.statistic.length; j++) {
                        var stt = acc.statistic[j];
                        var id = stt.type;
                        var info = salesParameters[id];
                        if (info) {
                            stt.title = info.title;
                        }
                    }
                }
            }

            return data;
        },
        _getParameters: function (params) {
            var defaultStatisticsInfo = workreportService.getStatisticsInfo();
            for (var i = 0; i < params.length; i++) {
                var param = params[i];
                var id = param.id + '';
                var newinfo = (defaultStatisticsInfo[id] ? defaultStatisticsInfo[id] : {});
                newinfo.title = param.parameterName;
                defaultStatisticsInfo[id] = newinfo;
            }
            workreportService.setStatisticsInfo(defaultStatisticsInfo);
            return defaultStatisticsInfo;
        },
        _afterPopup: function (winId) {
            var me = this;
            var win = $('#' + winId);
            win.on('click', 'h5.field_line', function () {
                var con = $(this).parent();
                if (con.hasClass('has_expand')) {
                    con.removeClass('has_expand');
                    return;
                }
                if (con.attr('hasdata') != 'true')return;//无数据，不加载
                win.find('.follow_process_content').removeClass('has_expand');
                var panel = con.find('.expand_panel');
                con.addClass('has_expand');
                var accountId = con.attr('accountId');
                me._initContent(accountId, panel);
            });
            var firstItem = win.find('.follow_process_content:first');
            firstItem.find('h5.field_line').click();//默认展开第一个
        },
        _initContent: function (accountId, panel) {
            var me = this;
            //systemItemId=36533&pageUserId=20016&startTime=0&endTime=1515180303661
            var opt = me.options;
            //alert(opt.belongTypeId)
            var submitData = {
                startTime: opt.start.unix() * 1000,
                endTime: opt.end.unix() * 1000,
                pageUserId: opt.ownerId,
                systemItemId: accountId
            };
            if (panel.attr('inited') == 'true')return;
            panel.attr('inited', 'true');

            panel.html('<div class="loading face_load hidden" style="display: block;"><span>'+rk.i18n('WORKREPORT_23')+'</span></div>');

            $.ajax({
                url: opt.detailUrl,
                data: submitData,
                dataType: 'json',
                type: 'GET'
            }).success(function (data) {
                if (!data)return;
                if (!data.data)return;
                panel.html('');

                panel.wrStoryListCtrl({
                    data: data.data
                });
                $(window).trigger('resize');//触发resize，popup可以重新定位一下，内容超高时有用
            });
        }
    });

});